package com.qding.park.manager.service.statis.facade.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationOperation;
import org.springframework.data.mongodb.core.aggregation.AggregationOperationContext;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.query.BasicQuery;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.mongodb.QueryBuilder;
import com.mongodb.util.JSON;
import com.qding.park.common.core.constant.MongodbConstant;
import com.qding.park.common.utils.DateUtils;
import com.qding.park.common.utils.ListUtil;
import com.qding.park.manager.cache.ParkCacheInter;
import com.qding.park.manager.common.pojo.ParkDataResult;
import com.qding.park.manager.facade.statis.service.StatisticsReportFacade;
import com.qding.park.manager.facade.statis.service.StatisticsReportFacade.InterMonth;
import com.qding.park.manager.pojo.TbParkspace;
import com.qding.park.manager.pojo.enums.ButtonTypeEnum;
import com.qding.park.manager.pojo.example.StatisInterDayExample;
import com.qding.park.manager.pojo.example.StatisInterMonthExample;
import com.qding.park.manager.pojo.example.StatisPassDayExample;
import com.qding.park.manager.pojo.example.StatisPassMonthExample;
import com.qding.park.manager.pojo.noncore.NcParkInoutInfo;
import com.qding.park.manager.pojo.noncore.NcPassDayReport;
import com.qding.park.manager.service.statis.biz.StatisticsReportBiz;

@Service("statisticsReportFacade")
public class StatisticsReportFacadeImpl implements StatisticsReportFacade {

	private static final Logger _log = LoggerFactory.getLogger(StatisticsReportFacadeImpl.class);

	@Autowired
	private MongoTemplate mongoTemplate;

	@Autowired
	private StatisticsReportBiz statisticsReportBiz;

	@Autowired
	private ParkCacheInter parkCacheInter;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.qding.park.manager.facade.statis.service.StatisticsReportFacade#
	 * interDay(com.qding.park.manager.pojo.example.StatisInterDayExample) 交互日报表
	 */
	@Override
	public ParkDataResult interDay(StatisInterDayExample example) {

		String tablename_terminallog = MongodbConstant.TABLENAME_TERMINALLOG;
		// 得到日期字符串
		String dataStr = DateUtils.formatDate(example.getDay(), DateUtils.DATE_FORMAT_DATEONLY);

		Criteria matchCrieria = new Criteria();
		List<String> parkIdlist = new ArrayList<String>();
		if (example.getParkId() != null) {
			parkIdlist.add(String.valueOf(example.getParkId()));
		} else if (!ListUtil.isEmpty(example.getInParks())) {
			parkIdlist.addAll(example.getStrInParks());
		}
		// if (!ListUtil.isEmpty(example.getInParks())) {
		// parkIdlist.addAll(example.getInParks());
		// }
		// if (example.getParkId() != null &&
		// !parkIdlist.contains(Integer.valueOf(example.getParkId()))) {
		// parkIdlist.add(Integer.valueOf(example.getParkId()));
		// }

		if (!ListUtil.isEmpty(parkIdlist) && !ListUtil.isEmpty(example.getNoInParks())) {
			// matchCrieria.and("parkIds").in(parkIdlist);
			// matchCrieria.and("parkId").in(parkIdlist);
			matchCrieria.andOperator(Criteria.where("parkId").in(parkIdlist),
					Criteria.where("parkId").nin(example.getNoInParks()));
		} else if (!ListUtil.isEmpty(parkIdlist)) {
			matchCrieria.and("parkId").in(parkIdlist);
		} else if (!ListUtil.isEmpty(example.getNoInParks())) {
			matchCrieria.and("parkId").nin(example.getNoInParks());
		}
		matchCrieria.and("createDate").is(dataStr);

		StringBuffer projectJsonStr = new StringBuffer();
		projectJsonStr.append("{ ");
		projectJsonStr.append(
				"  createDate:{$dateToString:{ format: \"%Y-%m-%d\", date: {$add:[\"$createDate\",+28800000]}}}, ");
		projectJsonStr.append("  parkIds :1, ");
		projectJsonStr.append("  parkId :1, ");
		projectJsonStr.append("  parkName :1, ");
		projectJsonStr.append("  buttonType : 1 ");
		projectJsonStr.append("} ");

		final DBObject project = new BasicDBObject("$project", JSON.parse(projectJsonStr.toString()));
		_log.info("$project is {}", project);

		StringBuffer groupJsonStr = new StringBuffer();
		groupJsonStr.append("{ ");
		groupJsonStr.append("  _id : {\"buttonType\":\"$buttonType\",\"parkId\":\"$parkId\"}, ");
		groupJsonStr.append("  total :{$sum:1} ");
		groupJsonStr.append("} ");
		final DBObject group = new BasicDBObject("$group", JSON.parse(groupJsonStr.toString()));
		_log.info("$group is {}", group);

		StringBuffer groupJsonStr_02 = new StringBuffer();
		groupJsonStr_02.append("{ ");
		groupJsonStr_02.append("  _id : {\"parkId\":\"$_id.parkId\"}, ");
		groupJsonStr_02.append("  itemsSold:{ $push:  { buttonType: \"$_id.buttonType\", total: \"$total\" } } ");
		groupJsonStr_02.append("} ");
		final DBObject group_02 = new BasicDBObject("$group", JSON.parse(groupJsonStr_02.toString()));
		_log.info("$group is {}", group_02);

		Aggregation agg = Aggregation.newAggregation(new AggregationOperation() {
			@Override
			public DBObject toDBObject(AggregationOperationContext context) {
				return project;
			}

		}, Aggregation.match(matchCrieria), new AggregationOperation() {
			@Override
			public DBObject toDBObject(AggregationOperationContext context) {
				return group;
			}

		}, new AggregationOperation() {
			@Override
			public DBObject toDBObject(AggregationOperationContext context) {
				return group_02;
			}

		});

		_log.info("$Aggregation is {}", agg.toString());

		AggregationResults<DBObject> aggResult = mongoTemplate.aggregate(agg, tablename_terminallog, DBObject.class);// DBObject

		List<DBObject> dbobjlist = aggResult.getMappedResults();
		List dataList = new ArrayList();
		int carLockNumTotal = 0;
		int feeNumTotal = 0;
		int generaNumTotal = 0;
		int hisRecordNumTotal = 0;
		int invitNumTotal = 0;
		int reserveNumTotal = 0;
		Map<String, InterMonth> monthMap = new HashMap<String, InterMonth>();
		for (DBObject info : dbobjlist) {
			int carLockNum = 0;
			int feeNum = 0;
			int generaNum = 0;
			int invitNum = 0;
			int reserveNum = 0;
			int hisRecordNum = 0;
			String parkName = "";
			// int buttonType = (int) info.get("buttonType");
			String parkIdStr = info.get("parkId") == null ? "" : (String) info.get("parkId");

			if (!StringUtils.isBlank(parkIdStr)) {
				int parkId = Integer.valueOf(parkIdStr);
				TbParkspace parkspace = parkCacheInter.queryParkSpace(parkId);
				parkName = parkspace == null ? "" : parkspace.getName();
			}
			List<DBObject> itemsSold = (List<DBObject>) info.get("itemsSold");
			for (DBObject dbobj : itemsSold) {
				int buttonType = (int) dbobj.get("buttonType");
				int num = (int) dbobj.get("total");
				if (ButtonTypeEnum.carLock.getType() == buttonType) {
					carLockNum = num;
				} else if (ButtonTypeEnum.fee.getType() == buttonType) {
					feeNum = num;
				} else if (ButtonTypeEnum.generaFee.getType() == buttonType) {
					generaNum = num;
				} else if (ButtonTypeEnum.hisRecord.getType() == buttonType) {
					hisRecordNum = num;
				} else if (ButtonTypeEnum.invit.getType() == buttonType) {
					invitNum = num;
				} else if (ButtonTypeEnum.reserve.getType() == buttonType) {
					reserveNum = num;
				}

			}
			// int total = (int) info.get("total");
			// if (buttonType == ButtonTypeEnum.carLock.getType()) {
			// carLockNum = total;
			// } else if (buttonType == ButtonTypeEnum.fee.getType()) {
			// feeNum = total;
			// } else if (buttonType == ButtonTypeEnum.generaFee.getType()) {
			// generaNum = total;
			// } else if (buttonType == ButtonTypeEnum.hisRecord.getType()) {
			// hisRecordNum = total;
			// } else if (buttonType == ButtonTypeEnum.invit.getType()) {
			// invitNum = total;
			// } else if (buttonType == ButtonTypeEnum.reserve.getType()) {
			// reserveNum = total;
			// }
			carLockNumTotal = carLockNumTotal + carLockNum;
			feeNumTotal = feeNumTotal + feeNum;
			generaNumTotal = generaNumTotal + generaNum;
			hisRecordNumTotal = hisRecordNum + hisRecordNumTotal;
			invitNumTotal = invitNumTotal + invitNum;
			reserveNumTotal = reserveNumTotal + reserveNum;
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("carLockNum", carLockNum);
			map.put("feeNum", feeNum);
			map.put("generaNum", generaNum);
			map.put("hisRecordNum", hisRecordNum);
			map.put("invitNum", invitNum);
			map.put("reserveNum", reserveNum);
			map.put("parkName", parkName);
			dataList.add(map);
		}

		Map<String, Object> data = new HashMap<String, Object>();
		data.put("list", dataList);
		data.put("carLockNumTotal", carLockNumTotal);
		data.put("feeNumTotal", feeNumTotal);
		data.put("generaNumTotal", generaNumTotal);
		data.put("hisRecordNumTotal", hisRecordNumTotal);
		data.put("invitNumTotal", invitNumTotal);
		data.put("reserveNumTotal", reserveNumTotal);

		data.put("currentPage", 1);
		data.put("numPerPage", 10);
		data.put("totalCount", dataList.size());
		data.put("hast", false);
		data.put("next", false);
		ParkDataResult parkDataResult = ParkDataResult.createObjDate();
		parkDataResult.putMapOnObj(data);
		return parkDataResult;
	}

	@Override
	public ParkDataResult interMonth(StatisInterMonthExample example, int jumpPage, int pageLength) {
		List<InterMonth> datalist = null;
		int total = 0;
		if (example.getEndTime() == null && example.getStartTime() == null) {
			// 按月查询
			datalist = statisticsReportBiz.interMonth_month(example, jumpPage, pageLength);
			total = statisticsReportBiz.interMonth_month_total(example);
		} else {
			// 两个时间都为空 按区间段查询
			datalist = statisticsReportBiz.interMonth_segment(example, jumpPage, pageLength);
			total = statisticsReportBiz.interMonth_segment_total(example);
		}
		int reserveNumTotal = 0;
		int feeNumTotal = 0;
		int invitNumTotal = 0;
		int generaNumTotal = 0;
		int carLockNumTotal = 0;
		int hisRecordNum = 0;
		for (InterMonth inter : datalist) {
			reserveNumTotal = reserveNumTotal + inter.getReserveNum();
			feeNumTotal = feeNumTotal + inter.getFeeNum();
			invitNumTotal = invitNumTotal + inter.getInvitNum();
			generaNumTotal = generaNumTotal + inter.getGeneraNum();
			carLockNumTotal = carLockNumTotal + inter.getCarLockNum();
			hisRecordNum = hisRecordNum + inter.getHisRecordNum();
		}

		ParkDataResult parkDataResult = ParkDataResult.createObjDate();
		parkDataResult.putDataOnObj("reserveNumTotal", reserveNumTotal);
		parkDataResult.putDataOnObj("feeNumTotal", feeNumTotal);
		parkDataResult.putDataOnObj("invitNumTotal", invitNumTotal);
		parkDataResult.putDataOnObj("generaNumTotal", generaNumTotal);
		parkDataResult.putDataOnObj("carLockNumTotal", carLockNumTotal);
		parkDataResult.putDataOnObj("hisRecordNum", hisRecordNum);

		parkDataResult.putDataOnObj("currentPage", jumpPage);
		parkDataResult.putDataOnObj("numPerPage", pageLength);
		parkDataResult.putDataOnObj("totalCount", total);
		parkDataResult.putDataOnObj("hast", jumpPage > 1 ? true : false);
		parkDataResult.putDataOnObj("next", jumpPage * pageLength < total ? true : false);
		parkDataResult.putDataOnObj("list", datalist);
		return parkDataResult;
	}

	@Override
	public List<InterMonth> inter_month_export(StatisInterMonthExample example) {
		List<InterMonth> datalist = new ArrayList<InterMonth>();
		String tablename_terminallog = MongodbConstant.TABLENAME_TERMINALLOG;

		/******* match条件 ***********/
		Criteria matchCrieria = new Criteria();
		// List<Integer> parkIdlist = new ArrayList<Integer>();
		// if (!ListUtil.isEmpty(example.getInParks())) {
		// parkIdlist.addAll(example.getInParks());
		// }
		// if (!StringUtils.isBlank(example.getParkId()) &&
		// !parkIdlist.contains(Integer.valueOf(example.getParkId()))) {
		// parkIdlist.add(Integer.valueOf(example.getParkId()));
		// }
		List<String> parkIdlist = new ArrayList<String>();
		if (!StringUtils.isBlank(example.getParkId())) {
			parkIdlist.add(example.getParkId());
		} else if (!ListUtil.isEmpty(example.getInParks())) {
			parkIdlist.addAll(example.getStrInParks());
		}
		// if (!ListUtil.isEmpty(parkIdlist)) {
		// matchCrieria.and("parkId").in(parkIdlist);
		// }

//		if (!ListUtil.isEmpty(parkIdlist) && !ListUtil.isEmpty(example.getNoInParks())) {
//			matchCrieria.andOperator(Criteria.where("parkId").in(parkIdlist),
//					Criteria.where("parkId").nin(example.getNoInParks()));
//		} else if (!ListUtil.isEmpty(parkIdlist)) {
//			matchCrieria.and("parkId").in(parkIdlist);
//		} else if (!ListUtil.isEmpty(example.getNoInParks())) {
//			matchCrieria.and("parkId").nin(example.getNoInParks());
//		}
//
//		if (example.getStartTime() != null && example.getEndTime() != null) {
//			matchCrieria.andOperator(Criteria.where("createDate").gte(example.getStartTime()),
//					Criteria.where("createDate").lte(example.getEndTime()));
//		} else if (example.getStartTime() != null) {
//			matchCrieria.andOperator(Criteria.where("createDate").gte(example.getStartTime()));
//		} else if (example.getEndTime() != null) {
//			matchCrieria.andOperator(Criteria.where("createDate").lte(example.getEndTime()));
//		}
		List<Criteria>   andCrieria=new ArrayList<Criteria>();
		if (!ListUtil.isEmpty(parkIdlist)&&!ListUtil.isEmpty(example.getNoInParks())) {
			andCrieria.add(Criteria.where("parkId").in(parkIdlist));
			andCrieria.add(Criteria.where("parkId").nin(example.getNoInParks()));
		}else if(!ListUtil.isEmpty(parkIdlist)){
			andCrieria.add(Criteria.where("parkId").in(parkIdlist));
		}else if(!ListUtil.isEmpty(example.getNoInParks())){
			andCrieria.add(Criteria.where("parkId").nin(example.getNoInParks()));
		}
		
		if (example.getStartTime() != null && example.getEndTime() != null) {
			andCrieria.add(Criteria.where("createDate").gte(example.getStartTime()));
			andCrieria.add(Criteria.where("createDate").lte(example.getEndTime()));
		} else if (example.getStartTime() != null) {
			andCrieria.add(Criteria.where("createDate").gte(example.getStartTime()));
		} else if (example.getEndTime() != null) {
			andCrieria.add(Criteria.where("createDate").lte(example.getEndTime()));
		}
		if(!ListUtil.isEmpty(andCrieria)){
			matchCrieria.andOperator(andCrieria.toArray(new Criteria[andCrieria.size()]));
		}
		/*********************/
		StringBuffer projectJsonStr = new StringBuffer();
		projectJsonStr.append("{ ");
		projectJsonStr.append(
				"  createDate:{$dateToString:{ format: \"%Y-%m-%d\", date:{$add:[ \"$createDate\",+28800000]}}}, ");
		projectJsonStr.append("  parkIds :1, ");
		projectJsonStr.append("  parkId :1, ");
		projectJsonStr.append("  buttonType : 1 ");
		projectJsonStr.append("} ");

		final DBObject project = new BasicDBObject("$project", JSON.parse(projectJsonStr.toString()));
		_log.info("$project is {}", project);

		StringBuffer groupJsonStr_1 = new StringBuffer();
		groupJsonStr_1.append("{ ");
		groupJsonStr_1.append(
				"  _id : {\"createDate\":\"$createDate\",\"parkId\":\"$parkId\", \"buttonType\":\"$buttonType\"}, ");
		groupJsonStr_1.append("  total :{$sum:1} ");
		groupJsonStr_1.append("} ");
		final DBObject group_1 = new BasicDBObject("$group", JSON.parse(groupJsonStr_1.toString()));
		_log.info("$group_1 is {}", group_1);

		StringBuffer groupJsonStr_2 = new StringBuffer();
		groupJsonStr_2.append("{ ");
		groupJsonStr_2.append("  _id : {\"createDate\":\"$_id.createDate\",\"parkId\":\"$_id.parkId\"}, ");
		groupJsonStr_2.append("  itemsSold:{ $push:  { buttonType: \"$_id.buttonType\", total: \"$total\" } }");
		groupJsonStr_2.append("} ");
		final DBObject group_2 = new BasicDBObject("$group", JSON.parse(groupJsonStr_2.toString()));
		_log.info("$group_2 is {}", group_2);

		// StringBuffer projectJsonStr = new StringBuffer();
		// projectJsonStr.append("{ ");
		// projectJsonStr.append(
		// " createDate:{$dateToString:{ format: \"%Y-%m-%d\", date:{$add:[
		// \"$createDate\",+28800000]}}}, ");
		// projectJsonStr.append(" parkId :1, ");
		// projectJsonStr.append(" buttonType : 1 ");
		// projectJsonStr.append("} ");
		// final DBObject project = new BasicDBObject("$project",
		// JSON.parse(projectJsonStr.toString()));
		// _log.info("$project is {}", project);
		//
		// StringBuffer groupJsonStr_1 = new StringBuffer();
		// groupJsonStr_1.append("{ ");
		// groupJsonStr_1.append(" _id : {\"createDate\":\"$createDate\",
		// \"buttonType\":\"$buttonType\"}, ");
		// groupJsonStr_1.append(" total :{$sum:1} ");
		// groupJsonStr_1.append("} ");
		// final DBObject group_1 = new BasicDBObject("$group",
		// JSON.parse(groupJsonStr_1.toString()));
		// _log.info("$group_1 is {}", group_1);
		//
		// StringBuffer groupJsonStr_2 = new StringBuffer();
		// groupJsonStr_2.append("{ ");
		// groupJsonStr_2.append(" _id : {\"createDate\":\"$_id.createDate\"},
		// ");
		// groupJsonStr_2.append(" itemsSold:{ $push: { buttonType:
		// \"$_id.buttonType\", total: \"$total\" } }");
		// groupJsonStr_2.append("} ");
		// final DBObject group_2 = new BasicDBObject("$group",
		// JSON.parse(groupJsonStr_2.toString()));
		// _log.info("$group_2 is {}", group_2);

		Aggregation agg = Aggregation.newAggregation(Aggregation.match(matchCrieria), new AggregationOperation() {
			@Override
			public DBObject toDBObject(AggregationOperationContext context) {
				return project;
			}

		}, new AggregationOperation() {
			@Override
			public DBObject toDBObject(AggregationOperationContext context) {
				return group_1;
			}

		}, new AggregationOperation() {
			@Override
			public DBObject toDBObject(AggregationOperationContext context) {
				return group_2;
			}

		}, Aggregation.sort(new Sort(Direction.DESC, "_id.createDate")));

		AggregationResults<DBObject> aggResult = mongoTemplate.aggregate(agg, tablename_terminallog, DBObject.class);

		List<DBObject> dbobjlist = aggResult.getMappedResults();
		Map<Integer, String> parkId2name = new HashMap<Integer, String>();
		for (DBObject info : dbobjlist) {
			_log.info(info.toString());
			InterMonth intermonth = new InterMonth();
			datalist.add(intermonth);
			String timeStr = (String) info.get("createDate");
			String parkIdStr = (String) info.get("parkId");
			String parkName = "";
			if (!StringUtils.isBlank(parkIdStr)) {
				if (parkId2name.containsKey(Integer.valueOf(parkIdStr))) {
					parkName = parkId2name.get(Integer.valueOf(parkIdStr));
				} else {
					TbParkspace tbParkspace = parkCacheInter.queryParkSpace(Integer.valueOf(parkIdStr));
					parkName = tbParkspace == null ? "" : tbParkspace.getName();
					parkId2name.put(Integer.valueOf(parkIdStr), parkName);
				}
			}
			intermonth.setParkName(parkName);
			intermonth.setTimeStr(timeStr);
			List<DBObject> itemsSold = (List<DBObject>) info.get("itemsSold");
			for (DBObject dbobj : itemsSold) {
				int buttonType = (int) dbobj.get("buttonType");
				int total = (int) dbobj.get("total");
				if (ButtonTypeEnum.carLock.getType() == buttonType) {
					intermonth.setCarLockNum(total);
				} else if (ButtonTypeEnum.fee.getType() == buttonType) {
					intermonth.setFeeNum(total);
				} else if (ButtonTypeEnum.generaFee.getType() == buttonType) {
					intermonth.setGeneraNum(total);
				} else if (ButtonTypeEnum.hisRecord.getType() == buttonType) {
					intermonth.setHisRecordNum(total);
				} else if (ButtonTypeEnum.invit.getType() == buttonType) {
					intermonth.setInvitNum(total);
				} else if (ButtonTypeEnum.reserve.getType() == buttonType) {
					intermonth.setReserveNum(total);
				}

			}
			// _log.info(info.toString());
			// InterMonth intermonth = new InterMonth();
			// datalist.add(intermonth);
			// String timeStr = (String) info.get("createDate");
			// intermonth.setTimeStr(timeStr);
			// String parkName="";
			// if(!StringUtils.isBlank(parkIdStr)){
			// if(parkId2name.containsKey(Integer.valueOf(parkIdStr))){
			// parkName=parkId2name.get(Integer.valueOf(parkIdStr));
			// }else{
			// TbParkspace
			// tbParkspace=parkCacheInter.queryParkSpace(Integer.valueOf(parkIdStr));
			// parkName=tbParkspace.getName();
			// parkId2name.put(Integer.valueOf(parkIdStr), parkName);
			// }
			// }
			// List<DBObject> itemsSold = (List<DBObject>)
			// info.get("itemsSold");
			// for (DBObject dbobj : itemsSold) {
			// int buttonType = (int) dbobj.get("buttonType");
			// int total = (int) dbobj.get("total");
			// if (ButtonTypeEnum.carLock.getType() == buttonType) {
			// intermonth.setCarLockNum(total);
			// } else if (ButtonTypeEnum.fee.getType() == buttonType) {
			// intermonth.setFeeNum(total);
			// } else if (ButtonTypeEnum.generaFee.getType() == buttonType) {
			// intermonth.setGeneraNum(total);
			// } else if (ButtonTypeEnum.hisRecord.getType() == buttonType) {
			// intermonth.setHisRecordNum(total);
			// } else if (ButtonTypeEnum.invit.getType() == buttonType) {
			// intermonth.setInvitNum(total);
			// } else if (ButtonTypeEnum.reserve.getType() == buttonType) {
			// intermonth.setReserveNum(total);
			// }
			//
			// }
		}

		return datalist;
	}

	@Override
	public ParkDataResult passDay(StatisPassDayExample example) {
		// 表名
		String tablenamePassDay = MongodbConstant.TABLENAME_PASS_DAY_REPORT;
		// 得到日期字符串
		String dataStr = DateUtils.formatDate(example.getDay(), DateUtils.DATE_FORMAT_DATEONLY);

		Criteria matchCrieria = new Criteria();
		List<Integer> parkIdlist = new ArrayList<Integer>();
		matchCrieria.where("_id").not();
		matchCrieria.and("oPTime").is(example.getDay());
		if (example.getParkId() != null) {
			parkIdlist.add(example.getParkId());
		} else if (!ListUtil.isEmpty(example.getInParks())) {
			parkIdlist.addAll(example.getInParks());
		}
		if (!ListUtil.isEmpty(parkIdlist)) {
			matchCrieria.and("parkId").in(parkIdlist);
		}

		// 构建查询条件

		Query query = Query.query(matchCrieria);
		_log.info(matchCrieria.toString());
		_log.info(query.toString());
		List<NcPassDayReport> infos = mongoTemplate.find(query, NcPassDayReport.class, tablenamePassDay);
		// for (NcPassDayReport info : dbobjlist) {
		int total = infos.size();
		List dataList = new ArrayList();
		int inCheckNumTotal = 0;// 进场次数总计
		int outCheckNumTotal = 0;// 出场次数总计
		int inManualNumTotal = 0;// 进场手动放行次数总计
		int outManualNumTotal = 0;// 出场手动放行次数总计
		int freeCarNumTotal = 0;// 免费车次数总计
		int parkFeeTotal = 0;// 停车费总计
		int monthShouldFeeTotal = 0;// 月费应收总计
		int monthFeeTotal = 0;// 月费实收总计
		int totalFeeTotal = 0;// 合计收费
		if (!ListUtil.isEmpty(infos)) {
			for (NcPassDayReport info : infos) {
				int freeCarNum = 0;// 免费车次数
				int inCheckNum = 0;// 进场次数
				int inManualNum = 0;// 进场手动放行次数
				int monthFee = 0;// 月牌实收(分)
				int monthShouldFee = 0;// 月牌应收(分)
				String operator = null;// 操作员
				int outCheckNum = 0;// 出场次数
				int outManualNum = 0;// 出场手动放行次数
				int parkFee = 0;// 停车费
				int totalFee = 0;// 合计费用(分)
				int workTime = 0;// 上班时间(秒)
				freeCarNum = info.getFreeTimes();
				inCheckNum = info.getEnterTimes();
				inManualNum = info.getEnManual();
				monthFee = info.getMonthPay();
				monthShouldFee = info.getCalcMonthPay();
				operator = info.getoPName();
				outCheckNum = info.getOutTimes();
				parkFee = info.getMoney();
				workTime = info.getDuring();
				outManualNum = info.getOutManual();
				totalFee = monthFee + parkFee;
				String parkName = info.getParkName();
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("freeCarNum", freeCarNum);
				map.put("inCheckNum", inCheckNum);
				map.put("inManualNum", inManualNum);
				map.put("monthFee", monthFee);
				map.put("monthShouldFee", monthShouldFee);
				map.put("outManualNum", outManualNum);
				map.put("operator", operator);
				map.put("outCheckNum", outCheckNum);
				map.put("parkFee", parkFee);
				map.put("workTime", workTime);
				map.put("totalFee", totalFee);
				map.put("parkName", parkName);
				dataList.add(map);

				inCheckNumTotal = inCheckNumTotal + inCheckNum;
				outCheckNumTotal = outCheckNumTotal + outCheckNum;
				inManualNumTotal = inManualNumTotal + inManualNum;
				outManualNumTotal = outManualNumTotal + outManualNum;
				freeCarNumTotal = freeCarNumTotal + freeCarNum;
				parkFeeTotal = parkFeeTotal + parkFee;
				monthShouldFeeTotal = monthShouldFeeTotal + monthShouldFee;
				monthFeeTotal = monthFeeTotal + monthFee;
				totalFeeTotal = totalFeeTotal + totalFee;
			}
		}
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("list", dataList);
		data.put("currentPage", 1);
		data.put("numPerPage", 10);
		data.put("totalCount", total);
		data.put("hast", false);
		data.put("next", false);

		data.put("inCheckNumTotal", inCheckNumTotal);
		data.put("outCheckNumTotal", outCheckNumTotal);
		data.put("inManualNumTotal", inManualNumTotal);
		data.put("outManualNumTotal", outManualNumTotal);
		data.put("freeCarNumTotal", freeCarNumTotal);
		data.put("parkFeeTotal", parkFeeTotal);
		data.put("monthShouldFeeTotal", monthShouldFeeTotal);
		data.put("monthFeeTotal", monthFeeTotal);
		data.put("totalFeeTotal", totalFeeTotal);
		ParkDataResult parkDataResult = ParkDataResult.createObjDate();
		parkDataResult.putMapOnObj(data);
		return parkDataResult;
	}

	@Override
	public ParkDataResult passMonth(StatisPassMonthExample example, int jumpPage, int pageLength) {

		List<PassMonth> datalist = null;
		int total = 0;
		if (example.getEndTime() == null && example.getStartTime() == null) {
			// 按月查询
			datalist = statisticsReportBiz.passMonth_month(example, jumpPage, pageLength);
			total = statisticsReportBiz.passMonth_month_total(example);
		} else {
			// 两个时间都为空 按区间段查询
			datalist = statisticsReportBiz.passMonth_segment(example, jumpPage, pageLength);
			total = statisticsReportBiz.passMonth_segment_total(example);
		}
		int inCheckNumTotal = 0;// 进场次数总计
		int outCheckNumTotal = 0;// 出场次数总计
		int inManualNumTotal = 0;// 进场手动放行次数总计
		int outManualNumTotal = 0;// 出场手动放行次数总计
		int freeCarNumTotal = 0;// 免费车次数总计
		int parkFeeTotal = 0;// 停车费总计
		int monthShouldFeeTotal = 0;// 月费应收总计
		int monthFeeTotal = 0;// 月费实收总计
		int totalFeeTotal = 0;// 合计收费
		for (PassMonth pass : datalist) {
			inCheckNumTotal = inCheckNumTotal + pass.getInCheckNum();
			outCheckNumTotal = outCheckNumTotal + pass.getOutCheckNum();
			inManualNumTotal = inManualNumTotal + pass.getInManualNum();
			outManualNumTotal = outManualNumTotal + pass.getOutManualNum();
			freeCarNumTotal = freeCarNumTotal + pass.getFreeCarNum();
			parkFeeTotal = parkFeeTotal + pass.getParkFee();
			monthShouldFeeTotal = monthShouldFeeTotal + pass.getMonthShouldFee();
			monthFeeTotal = monthFeeTotal + pass.getMonthFee();
			totalFeeTotal = totalFeeTotal + pass.getTotalFee();
		}

		ParkDataResult parkDataResult = ParkDataResult.createObjDate();
		parkDataResult.putDataOnObj("inCheckNumTotal", inCheckNumTotal);
		parkDataResult.putDataOnObj("outCheckNumTotal", outCheckNumTotal);
		parkDataResult.putDataOnObj("inManualNumTotal", inManualNumTotal);
		parkDataResult.putDataOnObj("outManualNumTotal", outManualNumTotal);
		parkDataResult.putDataOnObj("freeCarNumTotal", freeCarNumTotal);
		parkDataResult.putDataOnObj("parkFeeTotal", parkFeeTotal);
		parkDataResult.putDataOnObj("monthShouldFeeTotal", monthShouldFeeTotal);
		parkDataResult.putDataOnObj("monthFeeTotal", monthFeeTotal);
		parkDataResult.putDataOnObj("totalFeeTotal", totalFeeTotal);

		parkDataResult.putDataOnObj("currentPage", jumpPage);
		parkDataResult.putDataOnObj("numPerPage", pageLength);
		parkDataResult.putDataOnObj("totalCount", total);
		parkDataResult.putDataOnObj("hast", jumpPage > 1 ? true : false);
		parkDataResult.putDataOnObj("next", jumpPage * pageLength < total ? true : false);
		parkDataResult.putDataOnObj("list", datalist);
		return parkDataResult;
	}

	@Override
	public List<PassMonth> pass_month_export(StatisPassMonthExample example) {
		List<PassMonth> datalist = new ArrayList<PassMonth>();
		String tablename_pass = MongodbConstant.TABLENAME_PASS_DAY_REPORT;
		// int total = statisticsReportBiz.passMonth_segment_total(example);
		int total = (int) statisticsReportBiz.passMonth_report_total(example);
		// 两个时间都为空 按区间段查询
		datalist = statisticsReportBiz.passMonth_segment(example, 1, total);

		return datalist;
	}

}
